<!doctype html>
<html lang="en" data-bs-theme="auto">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="description" content="">
  <title>添加栏目</title>

  <link href="/assets/dist/css/bootstrap.min.css" rel="stylesheet">
  <link rel="stylesheet" href="/assets/icons/bootstrap-icons.css">
  <link rel="stylesheet" href="/libs/tree/select-tree.css">
  
  <link href="/css/dashboard.css" rel="stylesheet">
  <script src="/js/jquery.min.js"></script>
  <script src="/libs/tree/select-tree.js"></script>
</head>

<body>

  <div class="container">
    <form class="row g-3 mt-1" id="form" method="post">
      <h3>添加栏目</h3>
      <div class="col-md-6">
        <label for="topicname" class="form-label">栏目名称</label>
        <input type="text" class="form-control" id="topicname" name="topicname">
      </div>
      <div class="col-md-6">
        <label for="inputEmail4" class="form-label">父栏目</label>
        <select class="form-select" id="parentid" name="parentid">
        </select>
      </div>

      <div class="col-md-4">
        <label for="topictype" class="form-label">栏目类型</label>

        <select id="topictype" class="form-select" name="topictype">
          <option  value="0">图文内容</option>
          <option  value="1">单页图文</option>
          <option  value="2">多页图文</option>
          <option  value="3">单页倒叙</option>
          <option  value="4">文档目录</option>
          <option  value="5">新闻列表</option>
          <option  value="6">新闻首页</option>
          <option  value="7">产品展示</option>
          <option  value="8">产品类别</option>
          <option  value="9">商品列表</option>
          <option  value="10">商品展示</option>
          
          <option  value="11">视频首页</option>
          <option  value="12">视频列表</option>
          <option  value="13">博客类型</option>
          <option  value="14">兴趣论坛</option>
          <option  value="15">报名表单</option>
          <option  value="16">数据查询</option>
          <option  value="17">跳转地址</option>
          <option  value="18">菜单下拉</option>
          <option  value="19">菜单弹出层</option>
          <option  value="20">会员注册</option>
          <option  value="21">会员登录</option>
          <option  value="22">会员信息</option>
          <option  value="23">购物车</option>
        </select>
      </div>
      <div class="col-md-2">
        <label for="topicstatus" class="form-label">状态</label>
        <div class="form-check form-switch mt-2">
          <input class="form-check-input" type="checkbox" role="switch" name="topicstatus" id="topicstatus" checked>
          <label class="form-check-label" for="topicstatus">是否显示</label>
        </div>

      </div>

      <div class="col-md-6">
        <label for="urlpath" class="form-label">栏目urlpath</label>
        <input type="text" class="form-control" id="urlpath" name="urlpath">
      </div>


      <div class="col-12">
        <label for="memo" class="form-label">备注</label>
        <input type="text" class="form-control" id="memo" name="memo" value="">
      </div>

      <div class="col-12">
        <button type="submit" class="btn btn-primary">添加</button>
      </div>
    </form>

  </div>
  <script src="/assets/dist/js/bootstrap.bundle.min.js"></script>
  <script lang="javascript">

    var topic_json = <% echo << obj["list"].to_json(); %> ;
    $('#parentid').append($('<option>').val("0").text("一级栏目").attr("selected", true));
    for (var i = 0; i < topic_json.length; i++) {
      if(topic_json[i].id==2)
      {
        $('#parentid').append($('<option>').val(topic_json[i].id).text(topic_json[i].value).data("pid", topic_json[i].parentid).attr("selected","selected"));
      }
      else
      {      
        $('#parentid').append($('<option>').val(topic_json[i].id).text(topic_json[i].value).data("pid", topic_json[i].parentid));
      }
   }

    var treeselect = $("#parentid").selectTree({ expandLevel: 1 });


    $("#form").on("submit", function (event) {
      event.preventDefault();

      var data = $(this).serialize();

      var obj = this;

      //得到序列化数据
      $.ajax({
        url: "/admin/addtopicpost",
        method: 'post',
        data: data,
        success: function (res) {
          //添加成功，这里演示
          console.log(res);
          if (res.code == 0) {
            topic_json = res.list;

            $('#parentid').empty();

            $('#parentid').append($('<option>').val("0").text("一级栏目").attr("selected", true));
            for (var i = 0; i < topic_json.length; i++) {
              $('#parentid').append($('<option>').val(topic_json[i].id).text(topic_json[i].value).data("pid", topic_json[i].parentid));
            }
            treeselect.clearSelect();
            // treeselect =$("#parentid").selectTree({ expandLevel: 1 });


          }
          $("#form").trigger("reset");
        }
      })
    });

  </script>
</body>

</html>